<?php


namespace App\Controller\AdminApi;


use App\Middleware\AdminApi\AdminJWTAuthMiddleware;
use App\Service\DashboardService;
use App\Utils\CustomUtils;
use Hyperf\Contract\TranslatorInterface;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\Controller;
use Hyperf\HttpServer\Annotation\Middleware;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Contract\RequestInterface;
use Hyperf\Logger\LoggerFactory;
use Hyperf\Utils\Arr;
use Hyperf\Validation\Contract\ValidatorFactoryInterface;

/**
 * Class DashBoardController
 *
 * @package App\Controller\AdminApi
 * @Controller(prefix="/admin/api/dashboard")
 * @Middleware(AdminJWTAuthMiddleware::class)
 */
class DashBoardController extends AdminBaseController {

    /**
     * @Inject
     * @var TranslatorInterface
     */
    private $translator;

    /**
     * @Inject()
     * @var ValidatorFactoryInterface
     */
    protected $validationFactory;

    /**
     * @var \Psr\Log\LoggerInterface
     */
    protected $logger;

    /**
     * @var DashboardService
     * @Inject()
     */
    protected $DashboardService;

    public function __construct (LoggerFactory $loggerFactory) {
        // 第一个参数对应日志的 name, 第二个参数对应 config/autoload/logger.php 内的 key
        $this->logger = $loggerFactory->get('log', 'default');
    }

    private function setLocale () {
        if ($this->request->header('locale') == 'en') {
            $this->translator->setLocale('en');
        }
    }


    /**
     * 销售看板数据
     * @PostMapping(path = "statSales")
     *
     * @param RequestInterface $request
     *
     * @return
     */
    public function statSales (RequestInterface $request) {

        $this->setlocale();

        $ext = $this->request->getAttribute('ext', []);

        if (Arr::get($ext, 'role.extraType') != 1) {
            $data = ['isSale' => false];
            return CustomUtils::buildSuccess($data, '非销售身份,无法查看销售统计');
        }

        $param = [
            'adminId'   => Arr::get($ext, 'user.id'),
            'mchIdList' => Arr::get($ext, 'mchIdList', []),
        ];

        return $this->DashboardService->platformStatSales($param);
    }
}
